/*
 * DateUtil.java,  Created on 2010-8-23
 */

package com.SkyAI.test.drools.stream;

import java.util.Date;
import java.lang.String;

/**
 * Title: Date operation class
 * <p>
 * Description: DateUtil is a 'Helper' for date operation,it includes different
 * format of date transaction (yyyyMMdd,yyyy-MM-dd,yyyyMMddHHmmss,yyyy-MM-dd
 * HH:mm:ss)
 * <p>
 * Copyright: Copyright (c) 2010
 * <p>
 * Company: SkyAI
 * <p>
 * 
 * @author skyzhou
 * @version 1.0
 * @since JDK1.5
 */

public class DateUtil
{
	
	// date format defination
	private static java.text.SimpleDateFormat sdfLong = new java.text.SimpleDateFormat(
			"yyyy-MM-dd");
	
	private static java.text.SimpleDateFormat sdfLongTimePlus = new java.text.SimpleDateFormat(
			"yyyy-MM-dd HH:mm:ss");
	
	/**
	 * Translate the date of String type to java.util.Date type, the input
	 * string date value format must be 'yyyy-MM-dd'
	 * 
	 * @param date
	 *            the date of String, format is 'yyyy-MM-dd'
	 * @return the Date object translate from String
	 * 
	 * @throws ParseException
	 *             parse date exception
	 */
	public static Date parseDate(String date) throws java.text.ParseException
	{
		if (date == null || "".equals(date))
			return null;
		return new Date(sdfLong.parse(date).getTime());
	}
	
	/**
	 * Translate the date of String type to java.util.Date type, the input
	 * string date value format must be 'yyyy-MM-dd HH:mm:ss'
	 * 
	 * @param dateTime
	 *            the date of String, format is 'yyyy-MM-dd HH:mm:ss'
	 * @return the Date object translate from String
	 * 
	 * @throws ParseException
	 *             parse date exception
	 */
	public static Date parseDateTime(String dateTime) throws java.text.ParseException
	{
		if (dateTime == null || "".equals(dateTime))
			return null;
		
		return new Date(sdfLongTimePlus.parse(dateTime).getTime());
	}
	
	/**
	 * Translate java.util.Date type date object to String , the out string date
	 * value format is 'yyyy-MM-dd'
	 * 
	 * @param date
	 *            date object
	 * @return the string date value, format is 'yyyy-MM-dd'
	 * 
	 */
	public static String formatDate(Date date)
	{
		if (date == null)
			return null;
		return sdfLong.format(date);
	}
	
	/**
	 * Translate java.util.Date type date object to String , the out string date
	 * value format is 'yyyy-MM-dd HH:mm:ss'
	 * 
	 * @param date
	 *            date object
	 * @return the string date value, format is 'yyyy-MM-dd HH:mm:ss'
	 * 
	 */
	public static String formatDateTime(Date date)
	{
		if (date == null)
			return null;
		return sdfLongTimePlus.format(date);
	}
	
}